using System.Collections;
using UnityEngine;

public abstract class EnemyBasic : MonoBehaviour
{
    public float health;
    public bool moveable = true;
    public float dietime;
    public float Runspeed;
    public float deltadistance;


    protected float RepelDistance;

    protected int RepelDir;
    public float maxhealth;
    public float stiffratio;
    public float repelratio;
    public float freeze=1;
    public float stifftimeex;
    protected int RepelTimesex = 0;
    //int face = 1;
    protected Animator animator;
    protected GameObject player;
    public Transform player_transform;
    public Transform ttransform;
    protected PlayerBasic playerbasic;
    public int flip;
    protected Rigidbody2D myRigidbody;
    protected AnimatorStateInfo stateinfo;
    public EnemyAttack enemyAttack;
    public float healthrecoveryratio;
    protected GameObject[] creations;
    //private bool getdamage;
    public void Start()
    {
        animator = GetComponent<Animator>();
        player = GameObject.FindWithTag("Player");
        playerbasic = player.GetComponent<PlayerBasic>();
        myRigidbody = GetComponent<Rigidbody2D>();
        player_transform = player.GetComponent<Transform>();
        ttransform = GetComponent<Transform>();
        flip = 1;
        enemyAttack = GetComponentInChildren<EnemyAttack>();
        enemyAttack.face = -1;
        stiffratio = 1;
        repelratio = 1;
        health *= EnemyData.healthratio;
    }
    //public IEnumerator Stiff()
    //{

    //myRigidbody.Sleep();

    //   yield return new WaitForSeconds(stifftime);
    // myRigidbody.WakeUp();
    //   freeze = 1;
    //   animator.SetBool("stiff", false);
    //}
    void OnTriggerStay2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Air"))
        {
            myRigidbody.velocity = Vector2.zero;
            RepelDistance = 0;
            RepelTimesex = 0;
            Vector3 temp = transform.position;
            temp.x += RepelDir * 0.5f;
            temp.y +=  0.5f;
            transform.position = temp;
        }
        if (other.gameObject.CompareTag("Ground"))
        {
            myRigidbody.velocity = Vector2.zero;
            Vector3 temp = transform.position;
            temp.y += 0.5f;
            transform.position = temp;
        }
    }
    public virtual void TakeDamage(float damage,int face,int repeltimes,float repeldistance,float stifftime)
    {
        freeze = 0;
        health -= damage;
        playerbasic.Recovery(damage * playerbasic.healthrecoveryratio);
        RepelTimesex = repeltimes;
        RepelDir = face;
        stifftimeex = stiffratio*stifftime;
        RepelDistance = repelratio*repeldistance;
        Vector2 vel = new Vector2(0, myRigidbody.velocity.y);
        myRigidbody.velocity = vel;
        if (health <= 0)
        {
            animator.SetBool("stiff", false);
            
            stifftimeex = 999999;
            animator.SetTrigger("die");
            Invoke("Killplayer", dietime);
        }
        else animator.SetBool("stiff",true);
        
        //StartCoroutine(Stiff());
    }
    protected void Killplayer()
    {
        Vector2 vel = new Vector2(0, myRigidbody.velocity.y);
        myRigidbody.velocity = vel;
        Destroy(gameObject);
    }
    protected void Flip()
    {
        if (flip*(player_transform.position.x - ttransform.position.x)<0)
        {

            ttransform.localRotation = Quaternion.Euler(0, 180, 0);
            enemyAttack.face = 1;

        }
        else if (flip*(player_transform.position.x- ttransform.position.x)>0)
        {
            ttransform.localRotation = Quaternion.Euler(0, 0, 0);
            enemyAttack.face = -1;
        }
    }
    protected void Move()
    {

        
        if(player_transform.position.x+deltadistance< ttransform.position.x)
        {

            animator.SetBool("move", true);
            Vector2 vel = new Vector2(-freeze*Runspeed, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }
        else if(player_transform.position.x-deltadistance > ttransform.position.x)
        {
            animator.SetBool("move", true);
            Vector2 vel = new Vector2(freeze*Runspeed, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }
        else
        {
            
            animator.SetBool("move", false);
            Vector2 vel = new Vector2(0, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }
    }
    protected void getplayer()
    {
        player_transform = player.GetComponent<Transform>();
        creations = GameObject.FindGameObjectsWithTag("Creation");
        foreach (GameObject creation in creations)
        {
            if (Mathf.Abs(creation.GetComponent<Transform>().position.x - transform.position.x) < Mathf.Abs(player_transform.position.x - transform.position.x))
            {
                player_transform = creation.GetComponent<Transform>();
            }
        }
    }
    protected void Update()
    {
        getplayer();
        if (RepelTimesex>0)
        {
            RepelTimesex--;
            Vector3 vector = ttransform.position;
            vector.x -= RepelDir * Time.deltaTime * RepelDistance;
            ttransform.position = vector;
        }
        if (stifftimeex > 0)
        {
            stifftimeex -= Time.deltaTime;
        }
        else
        {
            freeze = 1;
            animator.SetBool("stiff", false);
            Move();
            Flip();
        }
        if (transform.position.y < -20f)
        {
            Killplayer();
        }
    }
   
}
